package com.jy.modules.cims.component.sysAssist;

import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.service.TCMmTaskLogService;
import com.jy.modules.cims.data.common.dao.TCMmTaskLogDao;
import com.jy.modules.cims.data.common.dto.TCMmCostPlanDTO;
import com.jy.modules.cims.data.common.dto.TCMmTaskLogDTO;
import com.jy.modules.cims.data.dao.CostPlanDao;
import com.jy.modules.cims.data.dto.CreditPackageDto;
import com.jy.platform.core.common.BaseDTO;

@Service("com.jy.modules.cims.component.sysAssist.TaskLogImpl")
public class TaskLogImpl {
	private static final Logger logger = LoggerFactory.getLogger(TaskLogImpl.class);
	
	@Autowired
	private CostPlanDao costPlanDao;

	@Autowired
	private TCMmTaskLogDao taskLogDao;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmTaskLogService")
	private TCMmTaskLogService tcmmTaskLogService;

	/**
	 * @description searchTCMmTaskLog
	 * @param cooperorgCode
	 * @param contractCode
	 * @param creditPackageName
	 * @param settleDate
	 * @return
	 * @throws Exception
	 */
	public List<TCMmTaskLogDTO> searchTCMmTaskLog(final String cooperorgCode, final String contractCode, final String creditPackageName,
			final String settleDate) throws Exception {
		TCMmTaskLogDTO queryTaskLogDto = new TCMmTaskLogDTO();
		queryTaskLogDto.setCooperorgCode(cooperorgCode);
		queryTaskLogDto.setContractCode(contractCode);
		queryTaskLogDto.setCreditPackageName(creditPackageName);
		queryTaskLogDto.setSettleDate(DateTimeUtils.parse(settleDate, "yyyy-MM-dd"));
		List<TCMmTaskLogDTO> tcmmTaskLogDTOList = tcmmTaskLogService.searchTCMmTaskLog(SearchParamUtil.getSearchParam(queryTaskLogDto));

		return tcmmTaskLogDTOList;
	}

	/**
	 * @description insertTCMmTaskLog
	 * @author shengzhoutao
	 * @param cooperorgCode
	 * @param contractCode
	 * @param creditPackageName
	 * @param settleDate
	 * @param exeStatus
	 * @return
	 * @throws Exception
	 */
	public Long insertTCMmTaskLog(final String cooperorgCode, final String contractCode, final String creditPackageName, final String settleDate,
			final String exeStatus) throws Exception {
		TCMmTaskLogDTO dto = new TCMmTaskLogDTO();
		dto.setCooperorgCode(cooperorgCode);
		dto.setContractCode(contractCode);
		dto.setCreditPackageName(creditPackageName);
		dto.setSettleDate(DateTimeUtils.parse(settleDate, "yyyy-MM-dd"));
		dto.setStartDate(new Timestamp(System.currentTimeMillis()));
		dto.setExeStatus(exeStatus);

		return tcmmTaskLogService.insertTCMmTaskLog(dto);
	}

	/**
	 * 
	 * @param cooperorgCode
	 * @param contractCode
	 * @param creditPackageName
	 * @param settleDate
	 * @throws Exception
	 */
	public void deleteTCMmTaskLog(final String cooperorgCode, final String contractCode, final String creditPackageName, final String settleDate) throws Exception {
		List<TCMmTaskLogDTO> tcmmTaskLogDTOList = this.searchTCMmTaskLog(cooperorgCode, contractCode, creditPackageName, settleDate);
		if (CollectionsUtils.isEmpty(tcmmTaskLogDTOList)) {
			return ;
		}
		
		for(TCMmTaskLogDTO taskLogDTO:tcmmTaskLogDTOList){
			tcmmTaskLogService.deleteTCMmTaskLogByID(new BaseDTO(), taskLogDTO.getId().toString());
		}
	}
	
	/**
	 * @description updateTCMmTaskLogExeStatus
	 * @author shengzhoutao
	 * @param id
	 * @param exeStatus
	 * @param errorInfo
	 * @return
	 * @throws Exception
	 */
	public boolean updateTCMmTaskLogExeStatus(final Long id, final String exeStatus, final String errorInfo) throws Exception {
		TCMmTaskLogDTO dto = tcmmTaskLogService.queryTCMmTaskLogByPrimaryKey(id.toString());
		if (null == dto) {
			return false;
		}

		if (CimsConstant.EXE_STATUS_UNPROCESSED.equals(exeStatus)) {
			dto.setExeStatus(CimsConstant.EXE_STATUS_UNPROCESSED);
			dto.setStartDate(null);
			dto.setEndDate(null);
			dto.setErrorInfo("");
		} else {
			dto.setExeStatus(exeStatus);
			dto.setEndDate(new Timestamp(System.currentTimeMillis()));
			dto.setErrorInfo(errorInfo);
		}
		
		tcmmTaskLogService.updateTCMmTaskLog(dto);

		return true;
	}

	/**
	 * @description saveOrUpdateTCMmTaskLog
	 * @param tcmmTaskLogDTOList
	 * @param cooperorgCode
	 * @param contractCode
	 * @param creditPackageName
	 * @param settleDate
	 * @return Long
	 * @throws Exception
	 */
	public Long saveOrUpdateTCMmTaskLog(final List<TCMmTaskLogDTO> tcmmTaskLogDTOList, final String cooperorgCode, final String contractCode,
			final String creditPackageName, final String settleDate) throws Exception {
		Long id = null;
		// 不存在数据，就新增
		if (CollectionsUtils.isEmpty(tcmmTaskLogDTOList)) {
			id = insertTCMmTaskLog(cooperorgCode, contractCode, creditPackageName, settleDate, CimsConstant.EXE_STATUS_BEGIN);
		}
		// 存在数据，先验证执行状态，后修改执行状态
		else {
			TCMmTaskLogDTO hisTCMmTaskLogDTO = tcmmTaskLogDTOList.get(0);
			// 执行完成不能再执行
			if (CimsConstant.EXE_STATUS_END.equals(hisTCMmTaskLogDTO.getExeStatus())) {
				return id;
			}

			id = hisTCMmTaskLogDTO.getId();

			hisTCMmTaskLogDTO.setStartDate(new Timestamp(System.currentTimeMillis()));
			hisTCMmTaskLogDTO.setEndDate(null);
			hisTCMmTaskLogDTO.setExeStatus(CimsConstant.EXE_STATUS_BEGIN);
			hisTCMmTaskLogDTO.setErrorInfo("");
			tcmmTaskLogService.updateTCMmTaskLog(hisTCMmTaskLogDTO);
		}

		return id;
	}
	
	/**
	 * @description updateAmount
	 * @param cooperorgCode
	 * @param contractCode
	 * @param creditPackageName
	 * @param settleDate
	 * @throws Exception
	 */
	public void updateAmount(final String cooperorgCode, final String contractCode, final String creditPackageName, final String settleDate) throws Exception {
		if (!SimpleStringUtils.hasText(cooperorgCode) || !SimpleStringUtils.hasText(contractCode) || !SimpleStringUtils.hasText(creditPackageName) || !SimpleStringUtils.hasText(settleDate)) {
			return;
		}
		
		try {
			CreditPackageDto creditPackageDto = new CreditPackageDto();
			creditPackageDto.setCooperorgCode(cooperorgCode);
			creditPackageDto.setContractCode(contractCode);
			creditPackageDto.setCreditPackageName(creditPackageName);
			creditPackageDto.setSettleDate(settleDate);

			TCMmCostPlanDTO costPlanDTO = costPlanDao.statCostPlanForPackage(SearchParamUtil.getSearchParam(creditPackageDto));
			creditPackageDto.setMatchAmount(costPlanDTO.getMatchAmount());
			creditPackageDto.setReturnAmount(costPlanDTO.getReturnAmount());

			taskLogDao.updateAmount(SearchParamUtil.getSearchParam(creditPackageDto));

			creditPackageDto = null;
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	/**
	 * @description updateAmount
	 * @param creditPackageDto
	 * @throws Exception
	 */
	public void updateAmount(CreditPackageDto creditPackageDto) throws Exception {
		List<TCMmTaskLogDTO> dataList = tcmmTaskLogService.searchTCMmTaskLog(SearchParamUtil.getSearchParam(creditPackageDto));
		if (CollectionsUtils.isEmpty(dataList)) {
			return;
		}

		for (TCMmTaskLogDTO data : dataList) {
			updateAmount(data.getCooperorgCode(), data.getContractCode(), data.getCreditPackageName(), DateTimeUtils.formatDate(data.getSettleDate(), "yyyy-MM-dd"));
		}
	}
}
